package sound.main;
import graphics.system.Texture;
import graphics.system.TextureContainer;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import tools.Help;
import tools.Point;
public class Sound implements Serializable {
	private int sid;
	private boolean playing;
	private String name;
	private boolean loop;
	public Sound(String filename) {
		this.sid = SoundManager.getSource(filename);
		SoundManager.setBuffer(sid, filename, 1, 1);
		pause();
		this.name = filename;
		playing = false;
	}
	public void setVolume(float f) {
		SoundManager.setVolume(sid, f);
	}
	public void setPitch(float f) {
		SoundManager.setPitch(sid, f);
	}
	public void play() {
		if (!playing) {
			playing = true;
			SoundManager.play(sid);
		}
	}
	public void replay() {
		playing = true;
		SoundManager.play(sid);
	}
	public void pause() {
		playing = false;
		SoundManager.pause(sid);
	}
	public void setPos(Point pos) {
		SoundManager.setSourcePos(sid, pos);
	}
	public void setLoop(boolean loop) {
		SoundManager.setLoopSource(sid, loop);
		this.loop = loop;
	}
	private void writeObject(ObjectOutputStream out) throws IOException {
		out.writeBoolean(loop);
		out.writeChars(this.name);
	}
	private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
		this.loop = in.readBoolean();
		String n = "";
		try {
			while (true) {
				char c = in.readChar();
				n = n + c;
			}
		} catch (Exception e) {
		}
		this.sid = SoundManager.getSource(n);
		SoundManager.setBuffer(sid, n, 1, 1);
		pause();
		this.name = n;
		playing = false;
	}
}
